package com.fancy.learncenter.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.NGKBannerDataBean;
import com.fancy.learncenter.bean.NGKListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.ActPartyActivity;
import com.fancy.learncenter.ui.activity.CourseDetailActivity;
import com.fancy.learncenter.ui.activity.PlayAudioActivity;
import com.fancy.learncenter.ui.activity.PlayVedioActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.adapter.FragmentNGKRVAdapter;
import com.fancy.learncenter.ui.view.CustomFootView;
import com.fancy.learncenter.ui.view.CustomHeaderView;
import com.fancy.learncenter.ui.view.ZoomOutPageTransformer;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;


/**
 * 环球少年
 */
public class NGKFragment extends Fragment {

    View view;

    ViewPager viewPager;

    LinearLayout topLL;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    LayoutInflater inflater;
    List<View> vpView;

    LinearLayout topBanner;


    List<NGKBannerDataBean.BannerListBean> bannerData;
    ViewPagerAdapter bannerAdapter;

    SimpleDraweeView simpleDraweeView1;
    TextView name1;
    TextView content1;

    SimpleDraweeView simpleDraweeView2;
    TextView name2;
    TextView content2;

    RelativeLayout actParty;


    List<NGKListDataBean.RecommendListBean> listData;
    FragmentNGKRVAdapter listAdapter;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_ngk, container, false);
        ButterKnife.bind(this, view);

        this.inflater = inflater;
        initView();
        initBanner();
        initRecycleView();

        getBannerData();
        getListData(true);
        return view;
    }

    private void initView() {
        topBanner = (LinearLayout) inflater.inflate(R.layout.fragment_ngk_banner, null);
        topLL = (LinearLayout) topBanner.findViewById(R.id.top_LL);
        viewPager = (ViewPager) topBanner.findViewById(R.id.view_pager);
        simpleDraweeView1 = (SimpleDraweeView) topBanner.findViewById(R.id.simpleDraweeView);
        simpleDraweeView2 = (SimpleDraweeView) topBanner.findViewById(R.id.simpleDraweeView2);
        name1 = (TextView) topBanner.findViewById(R.id.name1);
        name2 = (TextView) topBanner.findViewById(R.id.name2);
        content1 = (TextView) topBanner.findViewById(R.id.content1);
        content2 = (TextView) topBanner.findViewById(R.id.content2);
        actParty = (RelativeLayout) topBanner.findViewById(R.id.act_party);
        actParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActPartyActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    private void initBanner() {
        bannerData = new ArrayList<>();
        vpView = new ArrayList<>();
        bannerAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(bannerAdapter);
        viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        viewPager.setOffscreenPageLimit(3);
    }

    private void initRecycleView() {

        listData = new ArrayList<>();

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);


        listAdapter = new FragmentNGKRVAdapter(getActivity(), listData);

        listAdapter.setCustomLoadMoreView(new CustomFootView(getActivity()));
        listAdapter.setHeaderView(topBanner, recyclerView);
        recyclerView.setAdapter(listAdapter);

        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                getListData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getListData(false);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class ViewPagerAdapter extends PagerAdapter {

        ViewPagerAdapter() {

        }

        @Override
        public int getCount() {
            return vpView.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = vpView.get(position);
            container.addView(view);
            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.simpleDraweeView);
            simpleDraweeView.setImageURI(bannerData.get(position).getCoverImgUrl());

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (bannerData.get(position).getFrontType() == 1) {

                        if (bannerData.get(position).getEntityType() == 1) {  //视频
                            Intent intent = new Intent(getContext(), PlayVedioActivity.class);
                            intent.putExtra(Constant.showResourceCoverImg, bannerData.get(position).getCoverImgUrl());
                            intent.putExtra(Constant.showResourceId, bannerData.get(position).getEntityId());
//                            intent.putExtra(Constant.showResourceName, bannerData.get(position).get);
//                        intent.putExtra(Constant.intro, item.getName());?
                            startActivity(intent);
                        } else if (bannerData.get(position).getEntityType() == 2) { //音频
                            Intent intent = new Intent(getContext(), PlayAudioActivity.class);
                            intent.putExtra(Constant.showResourceId, bannerData.get(position).getEntityId());
                            intent.putExtra(Constant.showResourceCoverImg, bannerData.get(position).getCoverImgUrl());
                            startActivity(intent);
                        } else if (bannerData.get(position).getEntityType() == 6) {  //课程
                            Intent intent = new Intent(getContext(), CourseDetailActivity.class);
                            intent.putExtra("type", bannerData.get(position).getEntityId());
                            startActivity(intent);
                        }

                    } else if (bannerData.get(position).getFrontType() == 2) {  //h5
                        Intent intent = new Intent(getContext(), MallCommonH5Activity.class);
                        intent.putExtra(MallCommonH5Activity.URL_KEY, bannerData.get(position).getLinkUrl());
                        startActivity(intent);
                    }
                }
            });

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }

    }

    private void getBannerData() {

        HttpMehtod.getInstance().getNGKBanner(new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<NGKBannerDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), NGKBannerDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<NGKBannerDataBean>() {
                    @Override
                    public void success(NGKBannerDataBean dataBean) {


                        bannerData.addAll(dataBean.getBannerList());
                        for (int i = 0; i < bannerData.size(); i++) {
                            vpView.add(inflater.inflate(R.layout.fragment_ngk_vp_item, null));
                        }
                        if (dataBean.getHomeTagList().size() == 2) {
                            simpleDraweeView1.setImageURI(dataBean.getHomeTagList().get(0).getImgUrl());
                            name1.setText(dataBean.getHomeTagList().get(0).getName());
                            content1.setText(dataBean.getHomeTagList().get(0).getEnglishhName());

                            simpleDraweeView2.setImageURI(dataBean.getHomeTagList().get(1).getImgUrl());
                            name2.setText(dataBean.getHomeTagList().get(1).getName());
                            content2.setText(dataBean.getHomeTagList().get(1).getEnglishhName());

                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();

                bannerAdapter.notifyDataSetChanged();
            }
        });
    }

    int curPage = 1;
    int pageSize = 10;

    private void getListData(final boolean refresh) {
        Map map = new HashMap();
        if (refresh) {
            curPage = 1;
        } else {
            curPage++;
        }
        map.put("curPage", curPage);
        map.put("pageSize", pageSize);

        HttpMehtod.getInstance().getNGKListData(map, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<NGKListDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), NGKListDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<NGKListDataBean>() {
                    @Override
                    public void success(NGKListDataBean dataBean) {

                        if (refresh) {
                            listAdapter.notifyDataSetChanged(dataBean.getRecommendList());
                            xRefreshView.stopRefresh();

                            if (dataBean.getRecommendList().size() < pageSize) {
                                xRefreshView.setLoadComplete(true);
                            } else {
                                xRefreshView.setLoadComplete(false);
                            }

                        } else {
                            listAdapter.addAll(dataBean.getRecommendList());
                            if (dataBean.getRecommendList().size() < pageSize) {
                                xRefreshView.setLoadComplete(true);
                            } else {
                                xRefreshView.stopLoadMore();
                            }

                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();

            }
        });
    }
}
